package rpcctrl

import "errors"

//Callback 回调函数
type Callback func(string) string

// RPCCtrl 控制块
type RPCCtrl struct {
	//Cmap回调
	Cmap map[string]Callback
}

//Reqest 控制命令
type Reqest struct {
	Cmd string
}

//Response 响应
type Response struct {
	Info string
}

var rpcCtrl = RPCCtrl{
	Cmap: make(map[string]Callback),
}

//Get 命令
func (c *RPCCtrl) Get(req Reqest, res *Response) error {
	cb, ok := c.Cmap[req.Cmd]
	if ok {
		res.Info = cb(req.Cmd)
		return nil
	}

	return errors.New("no cmd")
}

//RegistCallback 注册命令
func RegistCallback(cmd string, callback Callback) {
	rpcCtrl.Cmap[cmd] = callback
}

//GetRPCCtrl GetRPCCtrl
func GetRPCCtrl() *RPCCtrl {
	return &rpcCtrl
}
